package top.zhangjianyong.tools.service.mybatis.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.WaBill;
import top.zhangjianyong.tools.mapper.WaBillMapper;
import top.zhangjianyong.tools.service.mybatis.IWaBillService;
import top.zhangjianyong.tools.utils.StringUtil;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ： zhangjianyong
 * @date ： 2022/7/20 4:22 PM
 */
@Service
public class IWaBillServiceImpl
        extends ServiceImpl<WaBillMapper, WaBill>
        implements IWaBillService {

    @Override
    public WaBill getByOrderNoAndPlatform(String orderNo, String platform) {
        return lambdaQuery().eq(WaBill::getOrderNo, orderNo)
                .eq(WaBill::getPlatform, platform)
                .one();
    }

    @Override
    public LocalDateTime maxDate(Long userId) {
        return baseMapper.maxDate(userId);
    }

    @Override
    public LocalDateTime minDate(Long userId) {
        return baseMapper.minDate(userId);
    }

    @Override
    public List<WaBill> listBill(Long userId) {
        LocalDateTime start = baseMapper.minDate(userId);
        LocalDateTime end = baseMapper.maxDate(userId);

        return listBill(userId, start, end);
    }

    @Override
    public List<WaBill> listBill(Long userId, LocalDateTime start, LocalDateTime end) {
        return lambdaQuery()
                .eq(WaBill::getCreateBy, userId)
                .between(WaBill::getTradeTime, start, end)
                .ne(WaBill::getState, "关闭")
                .list();
    }

    @Override
    public Page<WaBill> rank(LocalDateTime start, LocalDateTime end, Long userId, String platform, String inOrOut, int page, int size) {
        LambdaQueryChainWrapper<WaBill> le = lambdaQuery()
                .ge(WaBill::getTradeTime, start)
                .le(WaBill::getTradeTime, end)
                .eq(WaBill::getCreateBy, userId)
                .eq(WaBill::getDelFlag, 0)
                .ne(WaBill::getState, "关闭");

        if (StringUtils.isNotBlank(platform)) {
            le.eq(WaBill::getPlatform, platform);
        }
        if (StringUtils.isNotBlank(inOrOut)) {
            le.eq(WaBill::getInOrOut, inOrOut);
        }

        le.orderByDesc(WaBill::getAmt);

        return le.page(new Page<>(page, size));

    }

    @Override
    public boolean updateStateByOrderNo(String originOrderNo, String state) {
        return lambdaUpdate().likeLeft(WaBill::getOrderNo, originOrderNo)
                .set(WaBill::getState, state)
                .update();
    }

    @Override
    public boolean updateState(String platform, String type, String inOrOut,
                               String counterparty, String counterpartyAccount,
                               String commodity, String state) {
        return lambdaUpdate().eq(WaBill::getPlatform, platform)
                .eq(WaBill::getType, type)
                .eq(WaBill::getInOrOut, inOrOut)
                .eq(WaBill::getCounterparty, counterparty)
                .eq(WaBill::getCounterpartyAccount, counterpartyAccount)
                .eq(WaBill::getCommodity, commodity)
                .set(WaBill::getState, state)
                .update();
    }
}
